package com.alstudio.ui.module.user;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.protocol.xmpp.data.economic.EconomicRecords;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.pojo.ALXmppEvent;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.utils.log.ALLog;
import com.alstudio.view.absadapter.ConsumptionAdapter;
import com.alstudio.view.listview.PullRefreshAndLoadMoreView;
import com.alstudio.view.listview.PullRefreshAndLoadMoreView.RankViewLoadStateListener;
import com.alstudio.view.popwindow.ALPopWindowUtils;
import com.loovee.imaohu.R;

/**
 * 账单记录、积分记录
 * 
 * @author Dengyh 2014-03-28
 */
public class MyConsumptionRecordsActivity extends TitleBarActivity {

	private boolean isPoint = false;
	private boolean isShowIngSelectGender = false;
	private PopupWindow discoverySelectPopWindow;
	private View discoverySelectView;

	private PullRefreshAndLoadMoreView pullRefreshAndLoadMoreView;
	private ConsumptionAdapter adapter;
	private ArrayList<EconomicRecords> records = new ArrayList<EconomicRecords>();

	private final int SHOW_TYPE_MONEY = 0;
	private final int SHOW_TYPE_POINT = 1;

	private final String REQUEST_TYPE_ALL = "all";
	private final String REQUEST_TYPE_CONSUME = "consume";
	private final String REQUEST_TYPE_CHARGE = "charge";
	private final String REQUEST_TYPE_AWARD = "award";

	private String REQUEST_TYPE = REQUEST_TYPE_ALL;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(this, getClass().getSimpleName());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#setupView(android.content.Context,
	 * java.lang.String)
	 */
	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setTitleBarActivityContentView(R.layout.my_consumption_records_layout);
		initUI();
		initData();
		if (isPoint) {
			setCenterTextViewMessage(R.string.TxtPointsHistoryList);
		} else {
			setCenterTextViewMessage(R.string.TxtConsumptionRecords);
			setCenterMsgDrawable(R.drawable.find_title_icon_down, RIGHT);
			setCenterLayoutClickListener(centerLayoutClickListener);
			initDiscoveryPopWindow();
		}
	}

	private void initUI() {
		initPullListView();
	}

	/**
	 * 初始化列表形式的发现下来刷新控件
	 * 
	 * @param parent
	 */
	private void initPullListView() {
		adapter = new ConsumptionAdapter(records);
		pullRefreshAndLoadMoreView = (PullRefreshAndLoadMoreView) findViewById(R.id.consumption_records_list);
		pullRefreshAndLoadMoreView.setAdapter(adapter);
		pullRefreshAndLoadMoreView.disablePullRefresh();
		pullRefreshAndLoadMoreView.enbaleItemClickListener(false);
		// 监听粉丝列表加载事件回调
		pullRefreshAndLoadMoreView
				.setLoadStateListener(fansViewLoadStateListener);

	}

	// 粉丝滑动加载事件回调
	private RankViewLoadStateListener fansViewLoadStateListener = new RankViewLoadStateListener() {

		@Override
		public void onRefresh(PullRefreshAndLoadMoreView view) {
			// TODO Auto-generated method stub
			requestData();
		}

		@Override
		public void onLoadMore(PullRefreshAndLoadMoreView view) {
			// TODO Auto-generated method stub
			requestData();
		}

	};

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#initData()
	 */
	@Override
	protected void initData() {
		isPoint = getIntent().getBooleanExtra("isPoint", false);
		if (ALLocalEnv.isSendXmppDataEnable()) {
			if (isPoint) {
				requestPointsData();
			} else {
				requestData();
			}
		}
	}

	/**
	 * 初始化发现选择对话框
	 */
	private void initDiscoveryPopWindow() {
		discoverySelectView = ALLocalEnv.getLocalEnv()
				.getGlobalLayoutInflater()
				.inflate(R.layout.consumption_select_popup, null);

		View v = discoverySelectView.findViewById(R.id.select_all_fl);
		v.setTag(0);
		v.setOnClickListener(popWindoClckListener);
		v = discoverySelectView.findViewById(R.id.select_consume_fl);
		v.setTag(1);
		v.setOnClickListener(popWindoClckListener);
		v = discoverySelectView.findViewById(R.id.select_recharge_fl);
		v.setTag(2);
		v.setOnClickListener(popWindoClckListener);
		v = discoverySelectView.findViewById(R.id.select_award_fl);
		v.setTag(3);
		v.setOnClickListener(popWindoClckListener);
		discoverySelectPopWindow = ALPopWindowUtils.createPopupWindowWithView(
				this, discoverySelectView);

		discoverySelectPopWindow.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				// TODO Auto-generated method stub
				exitBlur();
				invokeCenterLayoutClcik();

			}
		});
	}

	private OnClickListener popWindoClckListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			invokePopWindoClck((Integer) v.getTag());
		}
	};

	private void invokePopWindoClck(int pos) {
		discoverySelectPopWindow.dismiss();
		switch (pos) {
		case 0:
			REQUEST_TYPE = REQUEST_TYPE_ALL;
			setCenterTextViewMessage(R.string.TxtConsumptionRecords);
			requestData();
			break;
		case 1:
			REQUEST_TYPE = REQUEST_TYPE_CONSUME;
			setCenterTextViewMessage(R.string.TxtPopConsumptionSelectConsume);
			requestData();
			break;
		case 2:
			REQUEST_TYPE = REQUEST_TYPE_CHARGE;
			setCenterTextViewMessage(R.string.TxtPopConsumptionSelectRecharge);
			requestData();
			break;
		case 3:
			REQUEST_TYPE = REQUEST_TYPE_AWARD;
			setCenterTextViewMessage(R.string.TxtPopConsumptionSelectAward);
			requestData();
			break;
		}
	}

	/**************************************** title栏中间布局点击事件 *******************************************/
	private OnClickListener centerLayoutClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			invokeCenterLayoutClcik();
		}
	};

	/**
	 * 处理中间布局点击
	 */
	private void invokeCenterLayoutClcik() {

		if (isShowIngSelectGender) {
			// 正在显示选择对话框，那么隐藏他
			setCenterMsgDrawable(R.drawable.find_title_icon_down, RIGHT);
		} else {
			// 没有显示对话框，显示
			setCenterMsgDrawable(R.drawable.find_title_icon_up, RIGHT);
			ALPopWindowUtils.showPopWindow(discoverySelectPopWindow,
					center_layout);
			enterBlur();

		}
		isShowIngSelectGender = !isShowIngSelectGender;
	}

	private void requestData() {
		showLoadingProgress();
		ALUiXmppRequestCreator.postALUiXmppGetUserMoneyHistory(
				pullRefreshAndLoadMoreView.getStartIndex() + "",
				pullRefreshAndLoadMoreView.getEndIndex() + "", REQUEST_TYPE);
	}

	private void requestPointsData() {
		showLoadingProgress();
		ALUiXmppRequestCreator.postALUiXmppGetUserPointsHistory(
				pullRefreshAndLoadMoreView.getStartIndex() + "",
				pullRefreshAndLoadMoreView.endIndex + "");
	}

	@SuppressWarnings("unchecked")
	public void handleGetUserMoneyOrPointsHistoryEvent(ALXmppEvent data) {
		dismissLoadingProgress();
		if (data.getResponseCode() != ALXmppEvent.RESPONSE_OK) {
			pullRefreshAndLoadMoreView.showNoDataInfo(
					R.drawable.friend_default_03,
					getString(R.string.TxtMyConsumptionNoRecord));
			return;
		}
		records.clear();
		ArrayList<EconomicRecords> tmp = (ArrayList<EconomicRecords>) data.getData();
		records.addAll(tmp);
		tmp.clear();
		tmp = null;
		// 数据获取成功
		ALLog.d("获取账单记录成功");
		switch (data.getIntData()) {
		case SHOW_TYPE_MONEY:
			if (records.size() == 0) {
				pullRefreshAndLoadMoreView.showNoDataInfo(
						R.drawable.friend_default_03,
						getString(R.string.TxtMyConsumptionNoRecord));
				return;
			}
			break;
		case SHOW_TYPE_POINT:
			if (records.size() == 0) {
				pullRefreshAndLoadMoreView.showNoDataInfo(
						R.drawable.friend_default_03,
						getString(R.string.TxtMyConsumptionNoRecord));
				return;
			}
			break;
		}
		pullRefreshAndLoadMoreView.showRefereshListView();
		adapter.notifyDataSetChanged();
	}

}
